package com.juku2024.juku.analysis.source

import android.content.Context
import com.juku2024.juku.analysis.BaseHttpParse
import com.juku2024.juku.analysis.OnParseVideoListener
import com.juku2024.juku.db.entity.SourceEntity
import com.juku2024.juku.network.api.AnalysisApi
import com.juku2024.juku.network.retrofit.RetrofitUtils
import com.juku2024.juku.tools.LogC
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers

class PVHttpParse: BaseHttpParse() {

    override fun parseVideo(context: Context, url: String, source: SourceEntity, callback: OnParseVideoListener) {
        super.parseVideo(context, url, source, callback)
        val sourceName = source.sourceName
        val targetUrl = source.url + url
        disposable = RetrofitUtils.instance.getApi(AnalysisApi::class.java)
            .parsePV(targetUrl)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe({ model ->
                if (model?.code != 0) {
                    LogC.e("parse -> ${sourceName}源 -> http解析失败...")
                    callback.onFail(sourceName)
                    return@subscribe
                }
                val target = model.data?.formats?.lastOrNull()
                if (target?.url == null) {
                    LogC.e("parse -> ${sourceName}源 -> http解析失败...")
                    callback.onFail(sourceName)
                } else {
                    var headerMap: HashMap<String, String>?=null
                    if (target.http_headers != null) {
                        headerMap = HashMap()
                        if (target.http_headers?.accept != null) {
                            headerMap["Accept"] = target.http_headers?.accept!!
                        }
                        if (target.http_headers?.acceptLanguage != null) {
                            headerMap["Accept-Language"] = target.http_headers?.acceptLanguage!!
                        }
                        if (target.http_headers?.acceptEncoding != null) {
                            headerMap["Accept-Encoding"] = target.http_headers?.acceptEncoding!!
                        }
                        if (target.http_headers?.acceptCharset != null) {
                            headerMap["Accept-Charset"] = target.http_headers?.acceptCharset!!
                        }
                        if (target.http_headers?.userAgent != null) {
                            headerMap["User-Agent"] = target.http_headers?.userAgent!!
                        }
                        if (target.http_headers?.referer != null) {
                            headerMap["Referer"] = target.http_headers?.referer!!
                        }
                        if (target.http_headers?.cookie != null) {
                            headerMap["Cookie"] = target.http_headers?.cookie!!
                        }
                    }
                    callback.onSuccess(sourceName, target.url!!, headerMap)
                    analysisParseDuration(sourceName)
                }
            },{
                it?.printStackTrace()
                LogC.e("parse -> ${sourceName}源-> http解析失败...")
                callback.onFail(sourceName)
            })
    }
}